/*
 * XDVRenderer.cpp
 *
 *  Created on: May 21, 2013
 *      Author: daviD
 */

#include "XDVRenderer.h"
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>

#include "Utils/XDVShader.h"
#include "XDVDeferredRendering.h"
#include "XDVFBORenderTexture.h"

XDVRenderer::XDVRenderer()
{
	m_width = 500;
	m_height = 500;
	m_fborendertexture = new XDVFBORenderTexture(m_width, m_height);
	m_deferredrenderer = new XDVDeferredRendering(m_width, m_height,
			m_fborendertexture);
}

XDVRenderer::~XDVRenderer()
{
	delete m_fborendertexture;
	delete m_deferredrenderer;
}

inline void DrawDummyTriange()
{
	glBegin(GL_TRIANGLES);
	//glColor3f(1.0f, 0.0f, 0.0f);
	glVertex2i(0, 1);
	//glColor3f(0.0f, 1.0f, 0.0f);
	glVertex2i(-1, -1);
	//glColor3f(0.0f, 0.0f, 1.0f);
	glVertex2i(1, -1);
	glEnd();
}

void XDVRenderer::DoRender()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	//Renders to textures
	m_fborendertexture->start();
	GeometryPass();
	m_fborendertexture->stop();

	LightingPass();

	glFlush();
}

void XDVRenderer::GeometryPass()
{
	XDVShader shader;
	shader.SetShaders(
			"Shaders\\DeferredShading.vert",
			"Shaders\\DeferredShading.frag");
	//glUseProgramObjectARB(shader.GetProgramHandler());

	float worldMatrix[16];
	glGetFloatv(GL_MODELVIEW_MATRIX, worldMatrix);
	int worldMat = glGetUniformLocationARB(shader.GetProgramHandler(),"WorldMatrix");
	glUniformMatrix4fvARB ( worldMat, 1, false, worldMatrix);

	DrawDummyTriange();
}

void XDVRenderer::LightingPass()
{
	m_deferredrenderer->render();
}
